/*
 * @Author: 贺楠 1071780426@qq.com
 * @Date: 2024-03-21 10:13:47
 * @LastEditors: 贺楠 1071780426@qq.com
 * @LastEditTime: 2024-04-07 13:28:51
 * @FilePath: \newMould_BHZ\src\components\PCManage\detectionTask\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/HntManage/pouringOrder/WBS/index.vue";

import {
    saveDevice, //保存
    getDeviceByFid, //详情
    getDevicePageLists
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "showName",
        "status"
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            fCompanyids: null,
            fCompanynames: null,
            dialogHeight: null,
            formObj: [],
            WBSShow: false, // WBS
            formData: {
                deviceId: "", //传deviceId表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                projectId: "", //项目id
                projectName: "", //项目简称
                companyId: "", //标段id
                companyName: "", //标段简称
                checkType: "", //监测类型
                deviceNum: "", //设备编号
                deviceName: "", //设备名称
                deviceNo: "", //设备对接ID
                isStart: 1, //是否启用
                deviceLongitude: "", //经度
                deviceLatitude: "", //纬度
                deviceLocation: "", //地址
                warnParm1: "", //主钩吊重极限值(t)
                warnParm2: "", //预留副钩吊重极限值(t)
                warnParm3: "", //风速极限值(m/s)
                warnParm4: "", //大车支腿偏差极限值
                warnParm5: "" //预留字段
            },
            btnShow: true,
            currentDetail: null,
        };
    },
    components: {
        publicForm,
        CustomDialog
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.deviceId
                        })
                    }
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        showName: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    if (newVal == 0) { //环境设备管理
                        this.formObj = formJs.formObj1
                    } else if (newVal == 1) { //安全系统设置设备管理
                        this.formObj = formJs.formObj2

                    } else if (newVal == 2) { //安全系统设置预警设置龙门吊
                        this.formObj = formJs.formObj3

                    }
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));

        this.btnShow = true;
        this.formObj[2].inputButton = true;
        if (this.statusNum == 1) { //新增

            this.formData = this.$options.data.call(this).formData;
            this.formData.companyId = this.fCompanyids; //当前登录人id
            if (this.status == 0) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[0].disabled = true;
                this.formData.checkType="环境监测"

            } else if (this.status == 1) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
            } else if (this.status == 2) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[0].disabled = true;
                this.formObj[1].disabled = true;
            }
            this.btnShow = true
        } else if (this.statusNum == 2) { //编辑
            this.formData.deviceId = this.currentDetail.deviceId //按钮 
            // this.getDetail({
            //     fId: this.currentDetail.deviceId
            // })
            if (this.status == 0) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                this.formObj[0].disabled = true;
                this.formData.checkType="环境监测"
            } else if (this.status == 1) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
            } else if (this.status == 2) {
                this.formObj.map((item) => {
                    item.disabled = false;
                });
                // this.yj_searchForm(this.fCompanyids)
                this.formObj[0].disabled = true;
                this.formObj[1].disabled = true;
            }
        } else if (this.statusNum == 7) { //查看
            // this.getDetail({
            //     fId: this.currentDetail.deviceId
            // })
            this.formObj[2].inputButton = false;
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        selectChange(data, a, b) {
            console.log(data, a, b);
            //  if (b == "deviceNum") { //设备编号
            //     this.formData.fCheckcompany = data.label; //检测单位
            //     this.formData.fEquipname = data.value //设备名称
            // }
        },
        yj_searchForm(id) {
           
            var obj = {
                fCompanyid: id, //左侧选中组织机构id
                page: 1, //当前页码
                pageSize: 500, //每页显示条数
                keyWord: "", // 工程名称
                keyType: "龙门吊监测", //龙门吊监测，架桥机监测，塔吊监测，挂篮监测，环境监测
            }
            this.loading = true;
            var that = this;
            getDevicePageLists(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        this.formObj[0].option=[]
                        res.data.pageInfo.list.map((item=>{
                            this.formObj[0].option.push({
                                lable:item.deviceNum,
                                value:item.deviceName
                            })
                        }))
                        console.log(333,this.formObj[0].option);
                        // that.tableData = res.data.pageInfo.list;
                        // that.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    // this.$message.error("列表数据 接口出错", error.msg);
                    console.log("列表数据 接口出错" + error.msg);
                    this.loading = false;
                });
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getDeviceByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpMsDevice
                    this.formData = data
                    if (this.statusNum == 7) {
                        this.btnShow = false
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm() {
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            saveDevice(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
        },
    },
};